﻿using UnityEngine;
using System.Collections;

public class GamePlayScene : MonoBehaviour {

    public InGameStateManager _inGameStateMgr;
    public MessageViewControl _messageViewCtrl;
    public CharacterMovement _CharacterMovement;

	public TerrainLoader _terrainLoader;
	public CharacterManager _characterMgr;
    public CannonBall _cannonBall = null;

	public GamePlayMode _playMode = GamePlayMode.Single;

	private bool _bOnPressKey = false;
	private GameObject _keyObj = null;
		
    void Awake()
    {
        _inGameStateMgr = new InGameStateManager();
        _messageViewCtrl = new MessageViewControl();

		_characterMgr = new CharacterManager ();
        _CharacterMovement = new CharacterMovement();
        
    }

	void Start ()
    {
		// instert temporary data..
		CharacterData characterData = CharacterDataMgr.GetCharacterData (0);
		Character character = _characterMgr.AddCharacter (0, characterData);

        _inGameStateMgr.CreateState();
        // 일단 자기턴으로.
        _inGameStateMgr.ChangeState(InGameStateManager.StateType.SingleMyTurnState);
        _CharacterMovement.AddDownMove(character._collisionFloor);
		_terrainLoader.LoadTerrain (0);
	}
	
	void Update ()
    {
        _inGameStateMgr.UpdateState(Time.deltaTime);
        _messageViewCtrl.UpdatePop();

        _CharacterMovement.UpdateDownMove(Time.deltaTime);

		if (_bOnPressKey) 
		{
			if( _playMode == GamePlayMode.Single && 
			   _inGameStateMgr.GetStateType() == InGameStateManager.StateType.SingleMyTurnState )
			{
				SingleMyTurnState singleMyTurnState = (SingleMyTurnState)_inGameStateMgr.GetState();
				singleMyTurnState.Move( _keyObj );
			}
		}
	}

    public void SetCannonBall( CannonBall cannonBall )
    {
        _cannonBall = cannonBall;
    }

	public void OnPressInput( GameObject obj )
	{
		_bOnPressKey = true;
		_keyObj = obj;
	}

	public void OnReleaseInput()
	{
		_bOnPressKey = false;
		_keyObj = null;
	}
}
